#include <NewLifeDDK.h>
#include <string.h>
#include <errno.h>

bool RegisterIRQ(int count, u16 *IRQs)
{
	int ret;

	for(int i=0; i<count; i++)
	{
		__asm__ __volatile__(
			"int $0x90\t\n"
			"movl %%eax, %0"
			:"=m"(ret)
			:"a"(0x10000003),"b"(IRQs[i])
		);
		if(ret!=0)
			return false;
	}

	return true;
}

bool UnRegisterIRQ(int count, u16 *IRQs)
{
	int ret;

	for(int i=0; i<count; i++)
	{
		__asm__ __volatile__(
			"int $0x90\t\n"
			"movl %%eax, %0"
			:"=m"(ret)
			:"a"(0x10000004),"b"(IRQs[i])
		);
		if(ret!=0)
			return false;
	}

	return true;
}

bool RegisterPort(int count, u16 *ports)
{
/*
	struct message msg;
		
	while(count>=8)
	{
		msg.m_source = PID_INVALID;
		msg.m_type = MTYPE_USR_RequestIOPermit;
		msg.PARAM_count = 8;
		memcpy(msg.PARAM_ports, ports, 8*sizeof(u16));
		sendrec(KPID_SCHEDULE, &msg);
		if(msg.PARAM_ScheReply!=ESUCCEED)
		{
			errno = msg.PARAM_ScheReply;
			return false;
		}

		count-=8;
		ports += 8;
	}
	if(count)
	{
		msg.m_source = PID_INVALID;
		msg.m_type = MTYPE_USR_RequestIOPermit;
		msg.PARAM_count = count;
		memcpy(msg.PARAM_ports, ports, count*sizeof(u16));
		sendrec(KPID_SCHEDULE, &msg);
		if(msg.PARAM_ScheReply!=ESUCCEED)
		{
			errno = msg.PARAM_ScheReply;
			return false;
		}
	}
*/
	return true;
}

bool UnRegisterPort(int count, u16 *ports)
{
/*
	struct message msg;

	while(count>=8)
	{
		msg.m_source = PID_INVALID;
		msg.m_type = MTYPE_USR_ReleaseIOPermit;
		msg.PARAM_count = 8;
		memcpy(msg.PARAM_ports, ports, 8*sizeof(u16));
		sendrec(KPID_SCHEDULE, &msg);
		if(msg.PARAM_ScheReply!=ESUCCEED)
		{
			errno = msg.PARAM_ScheReply;
			return false;
		}
		count-=8;
		ports += 8;
	}
	if(count)
	{
		msg.PARAM_count = count;
		memcpy(msg.PARAM_ports, ports, count*sizeof(u16));
		sendrec(KPID_SCHEDULE, &msg);
		if(msg.PARAM_ScheReply!=ESUCCEED)
		{
			errno = msg.PARAM_ScheReply;
			return false;
		}
	}
*/
	return true;
}

bool MapAddress(u32 vaddr, u32 size, u32 paddr)
{
	int ret;
	__asm__ __volatile__(
		"int $0x90\t\n"
		"movl %%eax, %0"
		:"=m"(ret)
		:"a"(0x10000001),"b"(vaddr),"c"(size),"d"(paddr)
	);
	
	if(ret==0)
		return true;
	else
		return false;
}

bool UnmapAddress(u32 vaddr, u32 size)
{
	int ret;
	__asm__ __volatile__(
		"int $0x90\t\n"
		"movl %%eax, %0"
		:"=m"(ret)
		:"a"(0x10000002),"b"(vaddr),"c"(size)
	);
	
	if(ret==0)
		return true;
	else
		return false;
}

bool CreateDevice(const char * name, int cnt)
{
	__asm__ __volatile__("int $0x90"::"a"(0x4010),"b"(name),"c"(0),"d"(cnt));
	return true;
}

